<template>
  <el-container>
    <el-header>
      <div class="home-logo" @click="goHome">
        <div class="logoImg">
          <img src="../assets/logo.png" alt="" />
        </div>
        <div class="site-name">电商管理后台</div>
      </div>
      <el-button @click="onLogout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapseMenu ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="'iconfont ' + collapseIcon"></i>
        </div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapseMenu"
          :collapse-transition="false"
          router
          :default-active="activePage"
        >
          <el-submenu
            :index="menu.id.toString()"
            v-for="menu in menuList"
            :key="menu.id"
          >
            <template slot="title">
              <i :class="`iconfont ${menu.iconName}`"></i>
              <span>{{ menu.name }}</span>
            </template>

            <el-menu-item
              v-for="submenu in menu.subMenus"
              :key="submenu.id"
              :index="'/' + submenu.path"
              @click="saveActiveNav(menu, submenu)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ submenu.name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item v-for="path in pathList" :key="path">{{
            path
          }}</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
          <router-view></router-view>
        </el-card>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// import { notEmpty, notNull } from "../pugins/assert.js";
export default {
  data() {
    return {
      menuList: [],
      isCollapseMenu: false,
      asideWidth: "200px",
      collapseIcon: "icon-zhedie",
      activePage: "",
      pathList: ["欢迎"],
    };
  },
  created() {
    this.getMenus();
    this.setActiveNav();
  },
  methods: {
    onLogout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    /**
     * 获取菜单
     */
    async getMenus() {
      // let response = await this.$http.get("StudentDTOes");
      // console.log(response);
      let response = await this.$http.get("Menu");
      if (response.status == 200) {
        console.log(response);
        this.menuList = response.data.sort((a, b) => a.id - b.id);
      }
    },
    async test() {
      // let response = await this.$http.get("StudentDTOes");
      // console.log(response);
      // let response = await this.$http.get("Menu");
      // if (response.status == 200) {
      //   this.menuList = response.data;
      // }
      // console.log(response);
    },
    /**
     * 点击按钮 切换折叠
     */
    toggleCollapse() {
      this.isCollapseMenu = !this.isCollapseMenu;
      this.collapseIcon = this.isCollapseMenu ? "icon-zhedie2" : "icon-zhedie";
    },
    /**
     * 返回主页
     */
    goHome() {
      console.log(this.activePage);
      this.$router.push("/home");
      this.pathList = ["欢迎"];
      this.activePage = "";
      window.sessionStorage.removeItem("menuname");
      window.sessionStorage.removeItem("submenuname");
    },
    /**
     * 保存点击的菜单状态
     */
    saveActiveNav(menu, submenu) {
      this.activePage = "/" + submenu.path;
      window.sessionStorage.setItem("activePage", "/" + submenu.path);
      this.pathList[0] = menu.name;
      this.pathList[1] = submenu.name;
      window.sessionStorage.setItem("menuname", menu.name);
      window.sessionStorage.setItem("submenuname", submenu.name);
    },
    /**
     * 获取点击的菜单状态
     */
    setActiveNav() {
      this.activePage = window.sessionStorage.getItem("activePage");
      this.pathList[0] = window.sessionStorage.getItem("menuname");
      this.pathList[1] = window.sessionStorage.getItem("submenuname");
    },
    setBreadcrumb() {
      // pathList;
    },
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    height: 100%;
    width: 100%;
    padding-left: 0;
    color: white;
    background-color: #373d41;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .home-logo {
      width: 30%;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      cursor: pointer;
      .logoImg {
        //padding: 10px;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          height: 40px;
          width: 40px;
          border-radius: 50%;
          background-color: white;
          border: 4px solid whitesmoke;
          box-shadow: 0 0 10px #ddd;
        }
      }
      .site-name {
        padding-left: 1rem;
        font-size: 1.6rem;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    color: #909399;
    .toggle-button {
      background-color: #4a5064;

      text-align: center;
      line-height: 24px;
      font-size: 10px;
      cursor: pointer;
      letter-spacing: 0.2em; // 字间距
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
    .el-breadcrumb {
      margin-bottom: 15px;
      font-size: 12px;
    }
    .el-card {
      box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
    }
  }
}
</style>
